package com.shanling.shanlingcontroller.ui.fragment;

import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseMVPFragment;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.persenter.RegistPersenter;
import com.shanling.shanlingcontroller.persenter.contract.Registcontract;
import com.shanling.shanlingcontroller.utils.CountDownTimerUtils;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.utils.netstatus.NetUtils;
import com.shanling.shanlingcontroller.view.ClearableEditText;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ch1 on 2019/9/29.
 */

public class RegistFragment extends BaseMVPFragment<Registcontract.Presenter> implements Registcontract.View {
    @BindView(R.id.et_phone)
    ClearableEditText etPhone;
    @BindView(R.id.et_code)
    ClearableEditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.et_password)
    ClearableEditText etPassword;
    @BindView(R.id.iv_hide)
    ImageView ivHide;
    @BindView(R.id.et_confirm)
    ClearableEditText etConfirm;
    @BindView(R.id.iv_confirmhide)
    ImageView ivConfirmhide;
    @BindView(R.id.bt_regist)
    Button btRegist;
    @BindView(R.id.pb_regist)
    ProgressBar pbRegist;
    private boolean isHide = true;
    private boolean isConfrimHide = true;

    @Override
    public void registeredSuccess(String username, String psw) {
        ToastUtils.showLongToast(getActivity(), R.string.regist_success);
        pbRegist.setVisibility(View.GONE);
        btRegist.setVisibility(View.VISIBLE);
        etConfirm.setText("");
        etCode.setText("");
        etPassword.setText("");
        etPhone.setText("");
    }

    @Override
    public void registeredFaild(String msg) {
        pbRegist.setVisibility(View.GONE);
        btRegist.setVisibility(View.VISIBLE);
        ToastUtils.showToast(getActivity(), msg);

    }

    @Override
    public void getcodeSuccess(String code) {
        ToastUtils.showToast(getActivity(), R.string.send_success);

    }

    @Override
    public void getcodeFaild(String msg) {
    }

    @Override
    public void getTokenSuccess(String appToken) {
        preferenceUtil.setAppToken(appToken);
    }

    @Override
    public void getTokenFaild(String msg) {
        ToastUtils.showToast(getActivity(), msg);
    }

    @Override
    protected Registcontract.Presenter createPresenter() {
        return new RegistPersenter();
    }

    @Override
    protected void onLazyLoad() {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_regist;
    }

    @Override
    protected void initViewsAndEvents() {
        setHide(true, etPassword, ivHide);
        setHide(true, etConfirm, ivConfirmhide);
        etPassword.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});
        etConfirm.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});
    }


    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }

    @OnClick({R.id.tv_code, R.id.iv_hide, R.id.iv_confirmhide, R.id.bt_regist})
    public void onViewClicked(View view) {
        String username = etPhone.getText().toString().trim();
        String psw = etPassword.getText().toString().trim();
        String code = etCode.getText().toString().trim();
        String confirm = etConfirm.getText().toString().trim();
        switch (view.getId()) {
            case R.id.tv_code:
                if (TextUtils.isEmpty(username)) {
                    ToastUtils.showLongToast(getActivity(), R.string.regist_enter);
                    return;
                }
                boolean networkAvailable = NetUtils.isNetworkAvailable(getActivity());
                if(!networkAvailable){
                    ToastUtils.showToast(getActivity(),R.string.error1013);
                 return;
                }
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(getActivity(), tvCode, 60000, 1000);
                mCountDownTimerUtils.start();
                if (TextUtils.isEmpty(preferenceUtil.getAppToken())) {
                    mPresenter.getToken();
                } else {
                    mPresenter.getVerificationcode(username, 1);
                }
                break;
            case R.id.iv_hide:
                isHide = !isHide;
                setHide(isHide, etPassword, ivHide);
                break;
            case R.id.iv_confirmhide:
                isConfrimHide = !isConfrimHide;
                setHide(isConfrimHide, etConfirm, ivConfirmhide);
                break;
            case R.id.bt_regist:
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(psw) || TextUtils.isEmpty(confirm)) {
                    ToastUtils.showLongToast(getActivity(), R.string.enter);
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    ToastUtils.showLongToast(getActivity(), R.string.enter_verification);
                    return;
                }
                if (!psw.equals(confirm)) {
                    ToastUtils.showLongToast(getActivity(), R.string.wrong_password);
                    return;
                }
                if (TextUtils.isEmpty(preferenceUtil.getAppToken())) {
                    mPresenter.getToken();
                } else {
                    pbRegist.setVisibility(View.VISIBLE);
                    btRegist.setVisibility(View.GONE);
                    mPresenter.registered(username, psw, code);
                }
                break;
        }
    }

    public void setHide(boolean hide, ClearableEditText editText, ImageView imageView) {
        if (hide) {
            imageView.setSelected(false);
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);//设置密码不可见
        } else {
            imageView.setSelected(true);
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);//设置密码可见
        }
    }


}
